<template>
  <el-form-item label="透明度">
    <el-radio-group v-model="editorForm.opacity.isDefault" @change="handleDefault">
      <el-radio :label="true">默认</el-radio>
      <el-radio :label="false">自定义</el-radio>
    </el-radio-group>
    <div v-if="!editorForm.opacity.isDefault" class="custom-after">
      <el-select v-model="editorForm.opacity.value" placeholder="选择透明度" style="width: 90px" @change="changeOpacity">
        <el-option v-for="item in sizeOptions" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
    </div>
  </el-form-item>
</template>

<script setup>
import { inject, ref, onMounted } from 'vue'

const { editorForm, domList, changeDefault, backUpdate } = inject('commonForm')
// 初始化属性和元素对应关系
const bindDom = ref([])
const initDomMapping = () => {
  const bindKeys = [`${editorForm.value._key}.opacity.value`]
  domList.value.forEach(element => {
    const bindValue = element.getAttribute('_bind')
    bindKeys.forEach(key => {
      if (bindValue.includes(key)) {
        bindDom.value.push(element)
      }
    })
  })
}

const handleDefault = (val) => {
  if (val) {
    changeDefault('opacity')
  }
  changeOpacity()
}

const changeOpacity = () => {
  console.log(editorForm.value.opacity.value, bindDom.value, 565656565)
  bindDom.value.forEach(element => {
    element.style.opacity = editorForm.value.opacity.value
  })
  if (bindDom.value.length === 0) {
    backUpdate()
  }
}

const sizeOptions = ref([
  { label: '0%', value: 0 },
  { label: '10%', value: 0.1 },
  { label: '20%', value: 0.2 },
  { label: '30%', value: 0.3 },
  { label: '40%', value: 0.4 },
  { label: '50%', value: 0.5 },
  { label: '60%', value: 0.6 },
  { label: '70%', value: 0.7 },
  { label: '80%', value: 0.8 },
  { label: '90%', value: 0.9 },
  { label: '100%', value: 1 }
])

onMounted(() => {
  initDomMapping()
})
</script>

<style lang="scss" scoped></style>